home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1987 / 12 / naro / files.c < prev    next >
Text File  |  1987-12-21  |  16KB  |  168 lines

  1. #include <stdio.h>                                                                           
  2. #include <fcntl.h>                                                                           
  3. #include <sys\types.h>                                                                       
  4. #include <sys\stat.h>                                                                        
  5. #include <io.h>                                                                              
  6. #include <string.h>                                                                          
  7.                                                                                              
  8. #include "loc.h"                                                                             
  9. #include "externs.h"                                                                         
  10.                                                                                              
  11. #define  F_OPEN      O_RDONLY | O_BINARY                                                     
  12. #define  F_CREATE    O_CREAT | O_TRUNC | O_RDWR | O_BINARY                                   
  13.                                                                                              
  14.                                                                                              
  15. void  open_file_system(input_file)                                                           
  16. char  *input_file ;                                                                          
  17. {                                                                                            
  18.    char  *create_str = "Can't create %s" ;                                                   
  19.    char  *open_str = "Can't open %s" ;                                                       
  20.    char  errmsg[MAX_LINE] ;                                                                  
  21.    char  *filename_ext ;                                                                     
  22.                                                                                              
  23.    /*                                                                                        
  24.       This module is responsible for openning or creating all of the                         
  25.       files used by this utility.                                                            
  26.    */                                                                                        
  27.                                                                                              
  28.    /* Perform all the filename processing */                                                 
  29.    strcpy(module_name, strupr(input_file)) ;                                                 
  30.    strcpy(exe_fname, module_name) ;                                                          
  31.    strcat(strcpy(map_fname, exe_fname), ".MAP") ;                                            
  32.                                                                                              
  33.    if ((config == FALSE) || (strlen(config_fname) == 0))                                     
  34.       strcat(strcpy(config_fname, exe_fname), ".CFG") ;                                      
  35.    else                                                                                      
  36.       strupr(config_fname) ;                                                                 
  37.                                                                                              
  38.    if ((hex_name == FALSE) || (strlen(abs_fname) == 0))                                      
  39.       strcat(strcpy(abs_fname, exe_fname), ".HEX") ;                                         
  40.    else                                                                                      
  41.       strupr(abs_fname) ;                                                                    
  42.                                                                                              
  43.    strcat(strcpy(print_fname, exe_fname), ".LOC") ;                                          
  44.    strcat(exe_fname, ".EXE") ;                                                               
  45.                                                                                              
  46.    /* Create the temporary file used for segment fixups and location */                      
  47.    strcpy(tmp_fname, "LOCATE.$$$") ;                                                         
  48.    tmp_file = open(tmp_fname, F_CREATE, S_IWRITE) ;                                          
  49.    if (tmp_file == -1)   {                                                                   
  50.       sprintf(errmsg, create_str, tmp_fname) ;                                               
  51.       perror(errmsg) ;                                                                       
  52.       exit(1) ;                                                                              
  53.    }                                                                                         
  54.                                                                                              
  55.    /* Create the absolute output file */                                                     
  56.    abs_file = open(abs_fname, F_CREATE, S_IWRITE) ;                                          
  57.    if (abs_file == -1)   {                                                                   
  58.       sprintf(errmsg, create_str, abs_fname) ;                                               
  59.       perror(errmsg) ;                                                                       
  60.       exit(1) ;                                                                              
  61.    }                                                                                         
  62.                                                                                              
  63.    /* Open the .EXE file */                                                                  
  64.    exe_file = open(exe_fname, F_OPEN) ;                                                      
  65.    if (exe_file == -1)   {                                                                   
  66.       sprintf(errmsg, open_str, exe_fname) ;                                                 
  67.       perror(errmsg) ;                                                                       
  68.       exit(1) ;                                                                              
  69.    }                                                                                         
  70.                                                                                              
  71.    /* Create the locate map output file */                                                   
  72.    print_file = fopen(print_fname, "wt") ;                                                   
  73.    if (print_file == NULL)   {                                                               
  74.       sprintf(errmsg, open_str, print_fname) ;                                               
  75.       perror(errmsg) ;                                                                       
  76.       exit(1) ;                                                                              
  77.    }                                                                                         
  78.                                                                                              
  79.    /* Open the configuration file for reading */                                             
  80.    config_file = fopen(config_fname, "rt") ;                                                 
  81.    if (config_file == NULL)   {                                                              
  82.       sprintf(errmsg, open_str, config_fname) ;                                              
  83.       perror(errmsg) ;                                                                       
  84.       exit(1) ;                                                                              
  85.    }                                                                                         
  86.                                                                                              
  87.    /* Open the linker map file for reading */                                                
  88.    map_file = fopen(map_fname, "rt") ;                                                       
  89.    if (map_file == NULL)   {                                                                 
  90.       sprintf(errmsg, open_str, map_fname) ;                                                 
  91.       perror(errmsg) ;                                                                       
  92.       exit(1) ;                                                                              
  93.    }                                                                                         
  94.                                                                                              
  95.    return ;                                                                                  
  96. }                                                                                            
  97.                                                                                              
  98.                                                                                              
  99. void   close_file_system()                                                                   
  100. {                                                                                            
  101.    char  errmsg[MAX_LINE] ;                                                                  
  102.    char  *close_str = "Unable to close %s" ;                                                 
  103.    char  *delete_str = "Unable to delete %s" ;                                               
  104.                                                                                              
  105.    /*                                                                                        
  106.       This function is responsible for shutting down the file system                         
  107.       and cleaning up the temporary files.  All files opened for                             
  108.       reading are closed and all files open for writing are closed                           
  109.       (normal exit) and possibly deleted (control-C abort event).                            
  110.    */                                                                                        
  111.                                                                                              
  112.    /* Close the link map */                                                                  
  113.    if (fclose(map_file) != 0)   {                                                            
  114.       sprintf(errmsg, close_str, map_fname) ;                                                
  115.       perror(errmsg) ;                                                                       
  116.    }                                                                                         
  117.                                                                                              
  118.    /* Close the locate configuration file */                                                 
  119.    if (fclose(config_file) != 0)   {                                                         
  120.       sprintf(errmsg, close_str, config_fname) ;                                             
  121.       perror(errmsg) ;                                                                       
  122.    }                                                                                         
  123.                                                                                              
  124.    /* Close the .EXE file */                                                                 
  125.    if (close(exe_file) == -1)   {                                                            
  126.       sprintf(errmsg, close_str, exe_fname) ;                                                
  127.       perror(errmsg) ;                                                                       
  128.    }                                                                                         
  129.                                                                                              
  130.    /* Close the locate map */                                                                
  131.    if (fclose(print_file) != 0)   {                                                          
  132.       sprintf(errmsg, close_str, print_fname) ;                                              
  133.       perror(errmsg) ;                                                                       
  134.    }                                                                                         
  135.                                                                                              
  136.    /* Close the absolute or hex object module */                                             
  137.    if (close(abs_file) == -1)   {                                                            
  138.       sprintf(errmsg, close_str, abs_fname) ;                                                
  139.       perror(errmsg) ;                                                                       
  140.    }                                                                                         
  141.                                                                                              
  142.    if (user_abort == TRUE)   {                                                               
  143.       /* Delete the locate map */                                                            
  144.       if (remove(print_fname) == -1)   {                                                     
  145.          sprintf(errmsg, delete_str, print_fname) ;                                          
  146.          perror(errmsg) ;                                                                    
  147.       }                                                                                      
  148.                                                                                              
  149.       /* Delete the object file */                                                           
  150.       if (remove(abs_fname) == -1)   {                                                       
  151.          sprintf(errmsg, delete_str, abs_fname) ;                                            
  152.          perror(errmsg) ;                                                                    
  153.       }                                                                                      
  154.    }                                                                                         
  155.                                                                                              
  156.    /* Close and then delete the temporary file */                                            
  157.    if (close(tmp_file) == -1)   {                                                            
  158.       sprintf(errmsg, close_str, tmp_fname) ;                                                
  159.       perror(errmsg) ;                                                                       
  160.    }                                                                                         
  161.                                                                                              
  162.    if (remove(tmp_fname) == -1)   {                                                          
  163.       sprintf(errmsg, delete_str, tmp_fname) ;                                               
  164.       perror(errmsg) ;                                                                       
  165.    }                                                                                         
  166.                                                                                              
  167.    return ;                                                                                  
  168. }